<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\handlers\impl;

use lflow\access\QueryFilter;
use lflow\helper\AssertHelper;
use lflow\model\SubProcessModel;
use lflow\workflow\Execution;

/**
 * 启动子流程处理器
 *
 * @author Mr.April
 * @since  1.0
 */
class StartSubProcessHandler
{
    /**
     * @var \lflow\model\SubProcessModel
     */
    private SubProcessModel $model;

    public function __construct(SubProcessModel $model)
    {
        $this->model = $model;
    }

    /**
     * 子流程执行出处理
     *
     * @param \lflow\workflow\Execution $execution
     *
     * @throws \lflow\WorkFlowException
     */
    public function handle(Execution $execution)
    {
        $engine  = $execution->getEngine();
        $process = $engine->process()->getProcessByVersion($this->model->getProcessName(), $this->model->getVersion());
        $child   = $execution->createSubExecution($execution, $process, $this->model->getName());
        $order   = $engine->startInstanceByExecution($child);
        AssertHelper:: notNull($order, "子流程创建失败");
        $execution->addTasks($engine->query()->getActiveTasks((new QueryFilter())->setOrderId($order->getId())));
    }
}